<?php
namespace app\api\model;

use think\Model;
use think\exception\ValidateException;

class ReceiverInfo extends Model
{
    protected $name = 'receiver_info';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 新增收件人
     */
    public function receiverCreate($param)
    {
        // 验证数据
        try {
            validate([
                'appid'  => 'require', 
                'sys_number'  => 'require', 
                'app_type'  => 'require', 
            ])->message([
                'appid.require'  => 'appid必须传递',   
                'sys_number.require'  => 'sys_number必须传递',   
                'app_type.require'  => 'app_type必须传递',   
            ])->check($param);
        } catch (ValidateException $e) {
            return result_error($e->getError());
        }

        $data['appid'] = $param['appid'] ?? '';//appid
        $data['third_number'] = $param['third_number'] ?? '';//第三方配送平台订单号
        $data['sys_number'] = $param['sys_number'] ?? '';//系统订单号
        $data['call_number'] = $param['call_number'] ?? '';//调用方订单号
        $data['app_type'] = $param['app_type'] ?? '';//第三方平台类型 1:闪送, 2:达达,3:蜂鸟
        $data['to_address'] =  $param['to_address'] ?? '';//收件地址
        $data['to_address_detail'] =  $param['to_address_detail'] ?? '';//收件详细地址
        $data['to_receiver_name'] =  $param['to_receiver_name'] ?? '';//收件人姓名
        $data['to_mobile'] =  $param['to_mobile'] ?? '';//收件联系人
        $data['to_latitude'] =  $param['to_latitude'] ?? '';//收件纬度（百度坐标系）
        $data['to_longitude'] =  $param['to_longitude'] ?? '';//收件经度（百度坐标系）

        $data['order_no'] =  $param['order_no'] ?? '';//第三方平台流水号
        $data['good_type'] =  $param['good_type'] ?? '';//物品类型
        $data['weight'] =  $param['weight'] ?? '';//物品重量，单位：kg
        $data['remarks'] =  $param['remarks'] ?? '';//备注
        $data['insurance'] =  $daparama['insurance'] ?? '';//保险费用
        $data['insurance_pro_id'] =  $param['insurance_pro_id'] ?? '';//保险产品ID
        $data['addition_fee'] =  $param['addition_fee'] ?? '';//快速通道费，单位：分
        $data['ordering_source_type'] =  $param['ordering_source_type'] ?? '';//物品来源：1闪送，2百度外卖，3饿了么外卖，4美团外卖，5其他平台，6京东到家，7达达，8饿百
        $data['ordering_source_no'] =  $param['ordering_source_no'] ?? '';//物品来源流水号
        $data['goods_size_id'] =  $param['goods_size_id'] ?? '';//蛋糕尺寸
        $data['goods_price'] =  $param['goods_price'] ?? '';//投保金额，单位：分
        $data['quality_delivery'] =  $param['quality_delivery'] ?? '';//尊享送服务
        $data['insurance_flag'] =  $param['insurance_flag'] ?? '';//0:不投保;1:投保，默认值为0。投保金额以goodsPrice为准
        $data['expect_start_time'] =  $param['expect_start_time'] ?? '';//期望送达时间起始时间，毫秒级时间戳
        $data['expect_end_time'] =  $param['expect_end_time'] ?? '';//期望送达时间终止时间，毫秒级时间戳
            
        // 创建
        try {
            $result = $this->save($data);

            if ($result) {
                return result_success('success',$result);
            } else {
                return result_error('保存失败');
            }
        } catch (\Exception $e) {
            return result_error($e->getMessage());
        }
    }

    /**
     * 更新收件人
     */
    public function receiverUpdate($param)
    {
        if (empty($param['third_number'])&&empty($param['sys_number'])&&empty($param['call_number'])&&empty($param['id'])){
            return  result_error('缺少必要更新条件');
        }
        
        if(isset($param['to_address'])) $data['to_address'] = $param['to_address'];//收件地址
        if(isset($param['to_address_detail'])) $data['to_address_detail'] = $param['to_address_detail'];//收件详细地址
        if(isset($param['to_sender_name'])) $data['to_sender_name'] = $param['to_sender_name'];//收件人姓名
        if(isset($param['to_mobile'])) $data['to_mobile'] = $param['to_mobile'];//收件联系人
        if(isset($param['to_latitude'])) $data['to_latitude'] = $param['to_latitude'];//收件纬度（百度坐标系）
        if(isset($param['to_longitude'])) $data['to_longitude'] = $param['to_longitude'];//收件经度（百度坐标系）

        try {
           
            $find = $this
                    ->when(isset($param['third_number']),function ($query) use($param) {
                        $query->where('third_number', $param['third_number']);
                    })
                    ->when(isset($param['sys_number']),function ($query) use($param) {
                        $query->where('sys_number', $param['sys_number']);
                    })
                    ->when(isset($param['call_number']),function ($query) use($param) {
                        $query->where('call_number', $param['call_number']);
                    })
                    ->when(isset($param['id']),function ($query) use($param) {
                        $query->where('id', $param['id']);
                    })
                    ->find();
            if (!$find){
                 return  result_error('未查询到收件人');
            }  
            
            $result=$find->save($data);

            if ($result) {
               return result_success('success',$result);
            } else {
               return result_error('保存失败');
            }
        } catch (\Exception $e) {
             return result_error($e->getMessage());
        }
    }
}
